package com.project2.util;

import java.security.MessageDigest;


/**
 * @author : bunnyupup
 * @description : Md5信息摘要算法 对密码进行加密
 * @date : Created in 2022/8/16 15:08
 */
public class Md5Util {

    public static String getMd5(String content) throws Exception {
        //获得消息消息摘要算法
        MessageDigest messageDigest = MessageDigest.getInstance("md5");
        byte[] contentBytes = content.getBytes();
        byte[] resultBytes = messageDigest.digest(contentBytes);//byte 8 2^8 ff
        int length = resultBytes.length;
        System.out.println("length:" + length);
        StringBuffer stringBuffer = new StringBuffer();
        for (byte resultByte : resultBytes) {
            int temp = resultByte&0xff; //0-255
            String s = Integer.toHexString(temp); //0-15 0a  05 0-f
            if (s.length() == 1){
                stringBuffer.append(0);
            }
            stringBuffer.append(s);
        }
        return stringBuffer.toString();
    }

    public static String getMd5(String content,String salt) throws Exception{
        content = content + "c&b&j" + salt + "}";
        return getMd5(content);
    }
}
